//
//  RHAddPlantCell.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/7/24.
//

#import "RHEditPlantCell.h"
#import "RHAddPlantModel.h"

@interface RHEditPlantCell()<QMUITextFieldDelegate>

@property (nonatomic, strong) QMUILabel *priceLab;

@property (nonatomic, strong) MASConstraint *rightConstraint;

@end

@implementation RHEditPlantCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    
    return self;
}

-(void)showPriceUnit:(BOOL)state
{
    [self.rightConstraint uninstall];
    self.priceLab.hidden = !state;
    kWeakSelf;
    
    [_textField mas_updateConstraints:^(MASConstraintMaker *make) {
        if (state) {
            self.rightConstraint = make.right.equalTo(weakSelf.priceLab.mas_left).offset(-5);
        }else{
            self.rightConstraint =  make.right.mas_offset(-15);
        }
    }];
    
}

-(void)setPlantModel:(RHAddPlantModel *)plantModel
{
    _plantModel = plantModel;
    kWeakSelf;
//    self.arrowImg.hidden = !plantModel.isSelection;
    BOOL isPrice = [plantModel.tag isEqualToString:@"plant_price"];
    self.priceLab.hidden = !isPrice;
    [self.rightConstraint uninstall];
    [_textField mas_updateConstraints:^(MASConstraintMaker *make) {
        if (isPrice) {
            self.rightConstraint = make.right.equalTo(weakSelf.priceLab.mas_left).offset(-5);
        }else{
            self.rightConstraint = make.right.mas_offset(-15);
        }
    }];
    
    self.titleLab.text = plantModel.title;
    self.textField.text = plantModel.content;
    self.textField.placeholder = plantModel.placeHolder;
    self.textField.maximumTextLength = plantModel.maxLength?plantModel.maxLength:NSUIntegerMax;
    self.textField.keyboardType = UIKeyboardTypeDefault;
    if ([plantModel.tag isEqualToString:@"plant_price"]) {
        self.textField.keyboardType = UIKeyboardTypeDecimalPad;//数字键盘
    }
    self.textField.userInteractionEnabled = !plantModel.isSelection;
    if (plantModel.isCanEdit == NO) {
        self.textField.userInteractionEnabled = NO;
    }
    
    //枚举选择项
    if(plantModel.isSelection){
        [plantModel.optionList enumerateObjectsUsingBlock:^(RHOptionModel * _Nonnull optionModel, NSUInteger idx, BOOL * _Nonnull stop) {
            if (([plantModel.content integerValue] == optionModel.typeId) && plantModel.content.length>0) {
                weakSelf.textField.text = optionModel.typeName;
            }
        }];
    }
}

- (void)setupUI{
    
    [self setBackgroundColor:UIColor.clearColor];
    [self.contentView setBackgroundColor:UIColor.clearColor];
    
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:UIColor.whiteColor];
    [self.contentView addSubview:bgView];
    
    _titleLab = [[QMUILabel alloc] init];
    [_titleLab setTextAlignment:NSTextAlignmentLeft];
    [_titleLab setFont:[UIFont systemFontOfSize:14]];
    [_titleLab setTextColor:kCOLOR_HEX(@"3F4443")];
    _titleLab.numberOfLines = 3;
    _priceLab.adjustsFontSizeToFitWidth = YES;
    _priceLab.minimumScaleFactor = 0.8;
    [bgView addSubview:_titleLab];
    
    _textField = [[QMUITextField alloc] init];
    [_textField setTextAlignment:NSTextAlignmentRight];
    [_textField setTextColor:[kCOLOR_HEX(@"3F4443") colorWithAlphaComponent:0.8]];
    [_textField setFont:[UIFont systemFontOfSize:14]];
    [_textField setPlaceholder:@"Please input"];
    _textField.delegate = self;
    [bgView addSubview:_textField];
    
    UIView *lineV = [[UIView alloc] init];
    [lineV setBackgroundColor:CFCellLineColor];
    [bgView addSubview:lineV];
    
    
    kWeakSelf;
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0,15, 0, 15));
        make.height.mas_equalTo(50);
    }];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.centerY.mas_offset(0);
        make.width.mas_equalTo(150);
    }];
    
    
    _priceLab = [[QMUILabel alloc] init];
    [_priceLab setTextAlignment:NSTextAlignmentLeft];
    _priceLab.numberOfLines = 0;
    _priceLab.text = @"$/度".rh_localized;
    [_priceLab setFont:[UIFont systemFontOfSize:14]];
    [_priceLab setTextColor:kCOLOR_HEX(@"3F4443")];
    [bgView addSubview:_priceLab];
    CGFloat priceWidth = [_priceLab.text getStringWidthFont:_priceLab.font limitHeight:20]+2;
    [_priceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_offset(0);
        make.right.mas_offset(-5);
        make.width.offset(priceWidth);
    }];
    
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleLab.mas_right).offset(5);
        self.rightConstraint =  make.right.mas_offset(-15);
        make.height.mas_equalTo(30);
        make.centerY.mas_offset(0);
    }];
    
    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.bottom.mas_offset(0);
        make.height.mas_equalTo(1);
    }];
    
    [_titleLab setText:@"title"];
    
    [[_textField rac_textSignal]subscribeNext:^(NSString * _Nullable text) {
        if (!weakSelf.plantModel.isSelection) {//枚举项保存的是枚举值
            weakSelf.plantModel.content = text;
        }
    }];
}

-(BOOL)textField:(QMUITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //删除
    if ([string isEqualToString:@""]) {
        return YES;
    }
    if ([self.titleLab.text isEqualToString:@"电价配置".rh_localized]) {
        if ([textField.text containsString:@"."]) {
            //有小数点就不让再次输入小数点
            if ([string containsString:@"."]||textField.text.length == textField.maximumTextLength) {
                return NO;
            }
            //只能输入两位小数
            //小数点的位置
            NSRange point_range = [textField.text rangeOfString:@"."];
            //在小数点前面可以输入
            if (range.location <= point_range.location) {
                return YES;
            }
            NSString *pointValue = [textField.text substringFromIndex:point_range.location+1];
            NSLog(@"------>%@",pointValue);
            if (pointValue.length>=2) {
                return NO;
            }
            return YES;
        }else{
            //没有小数点存在,如果第一个输入的是小数点,自动补0
            if ([string isEqualToString:@"."]&&textField.text.length == 0) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    textField.text = [NSString stringWithFormat:@"0%@",string];
                });
                return YES;
            }
        }
    }else{
        //其他的按照最大长度限制
        if (textField.text.length>textField.maximumTextLength) {
            return NO;
        }
    }
    return YES;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
